package com.tanhua.server.service;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.tanhua.dubbo.api.*;
import com.tanhua.model.domain.Question;
import com.tanhua.model.domain.Settings;
import com.tanhua.model.domain.User;
import com.tanhua.model.domain.UserInfo;
import com.tanhua.model.vo.ErrorResult;
import com.tanhua.model.vo.PageResult;
import com.tanhua.model.vo.SettingsVo;
import com.tanhua.server.exeception.BusinessException;
import com.tanhua.server.interceptor.UserHolder;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

/**
 * @author SunSun
 * @date 2021/10/24
 */
@Service
public class SettingsService {

    @Autowired
    private RedisTemplate<String,String> redisTemplate;

    @DubboReference
    private UserApi userApi;
    @DubboReference
    private QuestionApi questionApi;
    @DubboReference
    private SettingsApi settingsApi;
    @DubboReference
    private BlackListApi blackListApi;

    public SettingsVo settings() {
        SettingsVo vo = new SettingsVo();
        Long userId = UserHolder.getUserId();
        String mobile = UserHolder.getMobile();
        vo.setId(userId);
        vo.setPhone(mobile);

        Question question = questionApi.findUserid(userId);

        String txt = question == null ? "你喜欢java吗" : question.getTxt();

        vo.setStrangerQuestion(txt);

        Settings settings = settingsApi.findUserid(userId);

        if (settings != null) {
            vo.setGonggaoNotification(settings.getGonggaoNotification());
            vo.setLikeNotification(settings.getLikeNotification());
            vo.setPinglunNotification(settings.getPinglunNotification());
        }

        return vo;
    }

    public void saveQuestion(String content) {
        Long userId = UserHolder.getUserId();
        Question question = questionApi.findUserid(userId);

        if (question == null) {
            question = new Question();
            question.setUserId(userId);
            question.setTxt(content);
            questionApi.save(question);
        } else {
            question.setTxt(content);
            questionApi.update(question);
        }

    }

    public void saveSettings(Map map) {
        boolean gonggaoNotification = (boolean) map.get("gonggaoNotification");
        boolean pinglunNotification = (boolean) map.get("pinglunNotification");
        boolean likeNotification = (boolean) map.get("likeNotification");
        Long userId = UserHolder.getUserId();
        Settings settings = settingsApi.findUserid(userId);
        if (settings == null) {
            settings = new Settings();
            settings.setUserId(userId);
            settings.setGonggaoNotification(gonggaoNotification);
            settings.setLikeNotification(likeNotification);
            settings.setPinglunNotification(pinglunNotification);
            settingsApi.save(settings);
        } else {
            settings.setGonggaoNotification(gonggaoNotification);
            settings.setLikeNotification(likeNotification);
            settings.setPinglunNotification(pinglunNotification);
            settingsApi.update(settings);
        }
    }

    public PageResult blacklist(int page, int pagesize) {

        Long userId = UserHolder.getUserId();

        IPage<UserInfo> iPage = blackListApi.findByUserId(userId, page, pagesize);

        PageResult pr = new PageResult(page, pagesize, iPage.getTotal(), iPage.getRecords());

        return pr;
    }

    public void deleteBlackList(Long uid) {

        Long userId = UserHolder.getUserId();

        blackListApi.delete(userId, uid);
    }

    public Map checkVerificationCode(String phone, String code) {

        String rediscode = redisTemplate.opsForValue().get("CHECK_CODE" + phone);

        if (rediscode.isEmpty() || !rediscode.equals(code)) {
            throw new BusinessException(ErrorResult.loginError());
        }
        redisTemplate.delete("CHECK_CODE" + phone);

        Map<String, Object> map1 = new HashMap();
        map1.put("verification",true);
        return map1;
    }

    public void savePhone(String phone, Long userId) {

        String mobile = UserHolder.getMobile();

        User byMobile = userApi.findByMobile(mobile);
        byMobile.setMobile(phone);
        userApi.update(byMobile);
    }
}
